<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Follow_product extends MY_Controller{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('t_follow_product');
		$this->load->model('t_product');
	}

	/**
	 * [register_follow_product description] follow product
	 * @return [type] [description]
	 */
	public function register_follow_product(){
		// validation user
		$post = parent::validation_user();

		$follow[FL_PRO_USER_ID] = $post[T_USERS][USER_ID];

		if(!isset($post[FL_PRO_PRO_ID]) || empty($post[FL_PRO_PRO_ID])){
			parent::print_result('Missing key or value '. FL_PRO_PRO_ID);exit();
		}else{
			$follow[FL_PRO_PRO_ID] = $post[FL_PRO_PRO_ID];
			$product = $this->t_product->get_data_by_id($follow[FL_PRO_PRO_ID]);
			if($product == null || count($product) == 0){
				parent::print_result('Product does not exist');exit();
			}
		}
		
		$followProduct =$this->t_follow_product->get_data_by_property($follow);
		$like = $product[0][PRODUCT_LIKE] + 1;
		if(empty($followProduct)){
			// insert follow
			$registed = $this->t_follow_product->set_data($follow,1);

			if($registed == null){
				parent::print_result('Follow failed');exit();
			}else{		
				$register[0][PRODUCT_LIKE] = $like;
				parent::print_result($registed[0], SUCCESS_VALUE);
			}
		}else if($followProduct[0][FL_PRO_UNFOLLOW] == 1){
			$this->t_follow_product->update_data_by_property(array(FL_PRO_UNFOLLOW => 0, FL_PRO_GET_NOTI => 1), $follow);
			$followProduct[0][PRODUCT_LIKE] = $like;
			parent::print_result($followProduct[0], SUCCESS_VALUE);
		}else{
			$followProduct[0][PRODUCT_LIKE] = $like;
			parent::print_result($followProduct[0], SUCCESS_VALUE);
		}

		$this->t_product->update_data_by_id(array(PRODUCT_LIKE => $like), $post[FL_PRO_PRO_ID]);
		// notification to own's product
		$this->push_noti_to_owns_product($follow[FL_PRO_PRO_ID], $post[T_USERS][USER_NAME] . PUSH_MESSAGE_FOLLOW_TOPIC, PUSH_FOLLOW_TOPIC);
		exit();
	}

	public function push_noti_to_owns_product($productId, $msg, $type){
		$this->load->model('t_product');
		$product = $this->t_product->get_data_by_id($productId);
		parent::push_notification_to_client_via_user_id($product[0][PRODUCT_ID], $msg, $type , $productId);
	}

	public function is_follow($user_id = -1, $product_id = -1){
		if($user_id == -1 || $product_id == -1){
			return 'Params missing';
		}
		$data[FL_PRO_USER_ID] = $user_id;
		$data[FL_PRO_PRO_ID]  = $product_id;

		$follow = $this->t_follow_product->get_data_by_property($data);
		if($follow == 0 || count($follow) == 0){
			return 0;
		}else if($follow[0][FL_PRO_UNFOLLOW] == 1){
			return 1;
		}else {
			return 2;
		}
	}

	/**
	 * [unfollow_product description] remove follow product
	 * @return [type] [description]
	 */
	public function unfollow_product(){
		$post = parent::validation_user();

		$data[FL_PRO_USER_ID] = $post[T_USERS][USER_ID];
		// var_dump($post);
		if(!isset($post[FL_PRO_PRO_ID]) || empty($post[FL_PRO_PRO_ID])){
			parent::print_result('Missing key or value '. FL_PRO_PRO_ID);exit();
		}else{
			$data[FL_PRO_PRO_ID] = $post[FL_PRO_PRO_ID];
		}

		$follow = $this->t_follow_product->get_data_by_property($data);
		if(empty($follow)){
			parent::print_result(0);exit();	
		}else{
			$this->t_follow_product->update_data_by_property(array(FL_PRO_GET_NOTI => 0, FL_PRO_UNFOLLOW => 1) ,$data);
			$product = $this->t_product->get_data_by_id($data[FL_PRO_PRO_ID]);
			$like = $product[0][PRODUCT_LIKE] - 1;
			$this->t_product->update_data_by_id(array(PRODUCT_LIKE => $like), $product[0][PRODUCT_ID]);
			
			$follow[0][PRODUCT_LIKE] = $like;
			parent::print_result($follow[0], SUCCESS_VALUE);exit();	
		}
	}

	public function product_following_total(){
		$post = parent::validation_user();
		$user_id = $post[T_USERS][USER_ID];

		$arrProduct = $this->t_follow_product->get_data_by_property(array(USER_ID => $user_id, FL_PRO_UNFOLLOW => 0));
		if(empty($arrProduct)){
			parent::print_result(0, SUCCESS_VALUE);
		}else{
			parent::print_result(count($arrProduct), SUCCESS_VALUE);
		}
		exit();
	}

	public function get_product_following(){
		// validation user
		$post = parent::validation_user();

		$limit = 50;
		if(isset($post['max_load'])){
			$last = $post['max_load'];
			if($last > 0){
				$limit = '('.$last .','. ($last+50).')' ;	
			}else{
				$limit = 50;
			}
			
		}

		$arrProduct = $this->t_follow_product->get_following_product($post[T_USERS][USER_ID] , $limit);
		if($arrProduct == null || count($arrProduct) == 0){
			parent::print_result('No products following');
		}else{
			parent::print_result($arrProduct, SUCCESS_VALUE);
		}
		exit();
	}

	public function is_follow_product(){
		$post = parent::validation_user();

		$user_id = $post[T_USERS][USER_ID];
		if(isset($post[FL_PRO_PRO_ID])){
			$product_id = $post[FL_PRO_PRO_ID];
		}else{
			parent::print_result(0,SUCCESS_VALUE);exit();
		}

		$query = $this->t_follow_product->get_data_by_property(array(USER_ID => $user_id, FL_PRO_PRO_ID => $product_id));
		if(empty($query)){
			parent::print_result(0,SUCCESS_VALUE);exit();	
		}else{
			if($query[0][FL_PRO_UNFOLLOW] == 1){
				parent::print_result(0,SUCCESS_VALUE);exit();		
			}else{
				parent::print_result(1,SUCCESS_VALUE);exit();	
			}
		}

	}
}